package services

import (
	"context"
	"errors"
	"feng-che.com/server/driver-quiz/internal/constants"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

// AdminCreate 创建服务
func (s *Service) AdminCreate(ctx context.Context) (*mongo.UpdateResult, error) {
	err := s.autoSetExamCourse()
	if err != nil {
		return nil, err
	}
	err = s.setFields()
	if err != nil {
		return nil, err
	}
	var opts []*options.UpdateOptions
	opts = append(opts, options.Update().SetUpsert(true))
	return s.Save(ctx)
}

func (s *Service) autoSetExamCourse() error {
	switch s.Nature {
	case constants.Subject2FirstRound:
		s.ExamCourse = constants.ExamCourse2
	case constants.Subject2Rounds:
		s.ExamCourse = constants.ExamCourse2
	case constants.Subject3FirstRound:
		s.ExamCourse = constants.ExamCourse3
	case constants.Subject3Rounds:
		s.ExamCourse = constants.ExamCourse3
	default:
		return errors.New("没有对应考试科目")
	}
	return nil
}

func (s *Service) setFields() error {
	s.Create()
	s.Price *= 100
	s.UnderlinePrice *= 100
	return nil
}

////////////////////////////////////////

func (s *Service) SetDuration(ctx context.Context, duration int) error {
	s.Duration = duration
	return s.Update(ctx, &bson.D{
		{"$set", bson.D{
			{"duration", s.Duration},
		}},
	})
}

func (s *Service) SetSharing(ctx context.Context) error {
	return s.Update(ctx, &bson.D{
		{"$set", bson.D{
			{"profit_sharing", s.ProfitSharing},
		}},
	})
}
